/**
 * 
 */
package com.pkdl.forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import com.pkdl.dao.ThuocDAO;
import com.pkdl.helpers.Validator;

/**
 * @author Administrator
 * 
 */
public class DlgCapNhatThemThuoc extends JDialog {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgCapNhatThemDichVu dialog = new DlgCapNhatThemDichVu(null, true);
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField txtSoLuong;
	public String MaThuoc;
	public String TenThuoc;
	private ThuocDAO tDAO;
	public int SoLuong = 0;
	public String ThuocStr;
	private JTextField txtTenThuoc;
	private JTextField txtMaThuoc;

	/**
	 * Create the dialog.
	 */
	public DlgCapNhatThemThuoc(Frame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Them thuoc");
		this.setBounds(100, 100, 450, 222);
		this.getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			panel.setBackground(SystemColor.textHighlight);
			this.getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new BorderLayout(0, 0));
			{
				JLabel lblThmDchV = new JLabel("TH\u00CAM THU\u1ED0C");
				lblThmDchV.setPreferredSize(new Dimension(31, 32));
				lblThmDchV.setHorizontalAlignment(SwingConstants.CENTER);
				lblThmDchV.setForeground(Color.WHITE);
				lblThmDchV.setFont(new Font("Tahoma", Font.BOLD, 18));
				panel.add(lblThmDchV, BorderLayout.NORTH);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setLayout(null);
			this.getContentPane().add(panel, BorderLayout.CENTER);
			{
				JLabel lblMDchV = new JLabel("T\u00EAn thu\u1ED1c");
				lblMDchV.setBounds(24, 53, 74, 14);
				panel.add(lblMDchV);
			}
			{
				JButton btnThem = new JButton("Th\u00EAm");
				btnThem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						AtomicReference<Object> errText = new AtomicReference<Object>(
								"");
						if (DlgCapNhatThemThuoc.this.validateInputData(errText)) {
							DlgCapNhatThemThuoc.this.SoLuong = Integer
									.parseInt(DlgCapNhatThemThuoc.this.txtSoLuong
											.getText());
							DlgCapNhatThemThuoc.this.dispose();
						} else {
							JOptionPane.showMessageDialog(null, errText.get(),
									"Announcement", JOptionPane.ERROR_MESSAGE);
							return;
						}
					}
				});
				btnThem.setIcon(new ImageIcon(DlgCapNhatThemDichVu.class
						.getResource("/com/pkdl/resources/001_01.gif")));
				btnThem.setBounds(292, 117, 127, 23);
				panel.add(btnThem);
			}
			{
				JLabel lblNgiKhm = new JLabel("S\u1ED1 l\u01B0\u1EE3ng");
				lblNgiKhm.setBounds(24, 81, 74, 14);
				panel.add(lblNgiKhm);
			}
			{
				this.txtSoLuong = new JTextField();
				this.txtSoLuong.setColumns(10);
				this.txtSoLuong.setBounds(107, 78, 121, 20);
				panel.add(this.txtSoLuong);
			}
			{
				this.txtTenThuoc = new JTextField();
				this.txtTenThuoc.setEditable(false);
				this.txtTenThuoc.setColumns(10);
				this.txtTenThuoc.setBounds(107, 50, 313, 20);
				panel.add(this.txtTenThuoc);
			}

			JLabel lblMa = new JLabel("M\u00E3 thu\u1ED1c");
			lblMa.setBounds(24, 25, 74, 14);
			panel.add(lblMa);

			this.txtMaThuoc = new JTextField();
			this.txtMaThuoc.setEditable(false);
			this.txtMaThuoc.setColumns(10);
			this.txtMaThuoc.setBounds(107, 22, 121, 20);
			panel.add(this.txtMaThuoc);
		}
		this.setLocationRelativeTo(null);
	}

	/**
	 * Load danh sach thuoc
	 */
	public void loadThongTin() {
		this.MaThuoc = this.ThuocStr.split("-")[0].trim();
		this.TenThuoc = this.ThuocStr.split("-")[1].trim();
		this.txtMaThuoc.setText(this.MaThuoc);
		this.txtTenThuoc.setText(this.TenThuoc);
		this.txtSoLuong.setText(String.valueOf(this.SoLuong));
	}

	public void loadThongTin2() {
		this.txtMaThuoc.setText(this.MaThuoc);
		this.txtTenThuoc.setText(this.TenThuoc);
		this.txtSoLuong.setText(String.valueOf(this.SoLuong));
	}

	private boolean validateInputData(AtomicReference<Object> errText) {
		if (this.txtSoLuong.getText().trim().equals("")) {
			errText.set("Ban chua nhap so luong");
			return false;
		}
		if (!Validator.isInteger(this.txtSoLuong.getText())) {
			errText.set("So luong khong hop le");
			return false;
		}
		return true;
	}
}
